package com.jgp.glmeeting.service.impl;

import com.jgp.glmeeting.mobile.commons.BaseResponse;
import com.jgp.glmeeting.mobile.commons.ErrorMsg;
import com.jgp.glmeeting.model.Arrangement;
import com.jgp.glmeeting.model.Participants;
import com.jgp.glmeeting.repository.ParticipantsRepository;
import com.jgp.glmeeting.service.ArrangementService;
import com.jgp.glmeeting.service.ParticipantsService;
import com.jgp.sys.common.query.Operator;
import com.jgp.sys.common.query.OrderList;
import com.jgp.sys.common.query.QueryFilterList;
import com.jgp.sys.ui.Pager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ParticipantsServiceImpl implements ParticipantsService {

    @Autowired
    private ParticipantsRepository participantsRepository;
    @Autowired
    private ArrangementService arrangementService;
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public Participants queryById(String id) {
        return participantsRepository.read(id);
    }

    @Override
    public void addParticipants(Participants p) {
        participantsRepository.createOrUpdate(p);
    }

    @Override
    public List<Participants> queryParticipants(Participants p, Pager pager) {
        OrderList orders = new OrderList();
        orders.addOrder("createDatetime","desc");
        String name=p.getName();
        String workUnit=p.getWorkUnit();
        QueryFilterList queryFilterList=new QueryFilterList();

        //以下加限制条件
        if(StringUtils.isNotBlank(name)){
            queryFilterList.addFilter("name","eq",name);

        }
        if(StringUtils.isNotBlank(workUnit)){
            queryFilterList.addFilter("workUnit","eq",workUnit);
        }
        return participantsRepository.read(queryFilterList,orders,pager);

    }

    @Override
    public void removeParticipants(String id) {
        participantsRepository.delete(id);
    }

    @Override
    public List<Participants> queryLastParticipants(Participants p, Pager pager, List<String> Participantsids) {
        OrderList orders = new OrderList();
        orders.addOrder("createDatetime","desc");

        String name=p.getName();
        String workUnit=p.getWorkUnit();
        QueryFilterList queryFilterList=new QueryFilterList();

        //以下加限制条件
        if(StringUtils.isNotBlank(name)){
            queryFilterList.addFilter("name","eq",name);

        }
        if(StringUtils.isNotBlank(workUnit)){
            queryFilterList.addFilter("workUnit","eq",workUnit);
        }
        if(CollectionUtils.isNotEmpty(Participantsids)){
            queryFilterList.addFilter("id","nin",Participantsids);
        }
        return participantsRepository.read(queryFilterList,orders,pager);

    }

    @Override
    public Participants queryParticipantsByOpenId(String openId) {
        List<Participants> list =  participantsRepository.read(new QueryFilterList().addFilter("openId", Operator.eq, openId));
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public BaseResponse bindParticipants(String openId, String mobilePhone) {
        List<Participants> list = participantsRepository.read(new QueryFilterList().addFilter("mobilePhone", Operator.eq, mobilePhone));
         if(CollectionUtils.isNotEmpty(list)) {
            Participants participants = list.get(0);
            participants.setOpenId(openId);
            participantsRepository.update(participants);

            BaseResponse response = new BaseResponse(200);
            response.setObj(participants);
            return response;
        }else{
            BaseResponse response = new BaseResponse(ErrorMsg.GET_PARTICIPANTS_FAIL_BY_MOBILEPHONE.getCode(),ErrorMsg.GET_PARTICIPANTS_FAIL_BY_MOBILEPHONE.getErrorMsg());
            return response;
        }
    }

    @Override
    public List<Participants> queryListByGroupAndId(String participantsId, String group) {
        List<Participants> list = new ArrayList<>();
        if(StringUtils.isBlank(participantsId)){
            list = participantsRepository.read(new QueryFilterList().addFilter("group", Operator.eq, group));
        }else {
            list = participantsRepository.read(new QueryFilterList().addFilter("id", Operator.ne, participantsId).addFilter("group", Operator.eq, group));
        }
        return list;
    }

    /**
     * 根据号码查询人员信息
     */
    @Override
    public Participants queryParticipantsBymobilePhone(String mobilePhone) {
        QueryFilterList queryFilterList=new QueryFilterList();
        queryFilterList.addFilter("mobilePhone","eq",mobilePhone);
       List<Participants> lists=participantsRepository.read(queryFilterList);
        if(lists.size()>0){
            return lists.get(0);
        }
       else{
            return null;
        }
    }

    @Override
    @Transactional
    public List<Participants> sign(List<String> ids, String group) {
        List<Participants> list = participantsRepository.read(new QueryFilterList().addFilter("group", Operator.eq, group));
        if(CollectionUtils.isNotEmpty(list)){
            for (Participants p : list) {
                if(ids.contains(p.getId())){
                    p.setIsSign(1);
                    participantsRepository.update(p);
                }
            }
        }
        return list;
    }

    @Transactional
    @Override
    public void editParticipantsForMobile(Participants participants) {
        participantsRepository.createOrUpdate(participants);
        List<Arrangement> list = arrangementService.queryListByParticipantsId(participants.getId());
        if(CollectionUtils.isNotEmpty(list)) {
            for (Arrangement a: list) {
                a.setMobilePhone(participants.getMobilePhone());
                arrangementService.addArrangement(a);
            }
        }
    }

    @Override
    public void updateInvoicedByIds(List<String> ids) {
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ids", ids);
        namedParameterJdbcTemplate.update("update gl_participants set ISINVOICED = 1 where ID in (:ids)", params);
    }


}
